package com.ucar.ui.login;

import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.ucar.R;
import com.ucar.net.BaseResponse;
import com.ucar.net.Callback;
import com.ucar.net.NetRequest;
import com.ucar.ui.base.BaseActivity;

public class EditPersonDataActivity extends BaseActivity implements
		OnClickListener, OnCheckedChangeListener {

	private EditText et_name;
	private Button save_myselfdate;
	private RadioGroup rg_gander_group;
	private String gender;
	private String token;
	private String name;
	private RadioButton radioButton_man;
	private RadioButton radioButton_woman;
	private String mSex;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setBaseTitleContent(R.layout.activity_myselfdata,
				R.string.edit_selfdata);

	}

	@Override
	public void onInit() {
		setLeftImage(R.drawable.icon_arrow);
	}

	@Override
	public void onFindViews() {
		et_name = find(R.id.et_name);
		rg_gander_group = find(R.id.rg_gander_group);
		save_myselfdate = find(R.id.save_myselfdate);
		radioButton_man = find(R.id.radioButton_man);
		radioButton_woman = find(R.id.radioButton_woman);
		gender = mSpUtil.getGender();
		if (TextUtils.equals(gender, "男")) {
			radioButton_man.setChecked(true);
		} else {
			radioButton_woman.setChecked(true);
		}

	}

	@Override
	public void onInitViewData() {

	}

	@Override
	public void onBindListener() {
		save_myselfdate.setOnClickListener(this);
		rg_gander_group.setOnCheckedChangeListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.save_myselfdate:
			name = et_name.getText().toString().trim();
			if (TextUtils.isEmpty(name)) {
				showToast(R.string.input_nameinfo);
				return;
			}
			savePersonData();
			break;

		default:
			break;
		}
	}

	private void savePersonData() {
		// Tag used to cancel the request
		String tag = "tag_save_persondata";
		NetRequest.getInstance(this).post(new Callback() {

			@Override
			public void onPreExecute() {
				toShowProgressMsg(getString(R.string.save));
			}

			@Override
			public void onFinished() {
				toCloseProgressMsg();
			}

			@Override
			public void onSuccess(BaseResponse response) {
				mSpUtil.setGender(mSex);
				showToast("修改成功！");
				finish();
			}

			@Override
			public void onFailed(String msg) {
				showToast(msg);
			}

			@Override
			public Map<String, String> getParams() {
				token = mSpUtil.getToken();
				name = et_name.getText().toString().trim();
				mSpUtil.setName(name);
				gender = mSpUtil.getGender();

				Map<String, String> params = new HashMap<String, String>();
				params.put("url", "app/editUser");
				params.put("namech", name);
				params.put("gender", gender);
				params.put("token", token);
				params.put("serverId", "");
				return params;
			}
		}, tag);
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// 获取变更后的选中项的ID
		int radioButtonId = group.getCheckedRadioButtonId();
		// 根据ID获取RadioButton的实例
		RadioButton rb = (RadioButton) EditPersonDataActivity.this
				.findViewById(radioButtonId);
		// 更新文本内容，以符合选中项
		// gender.setText("您的性别是：" + rb.getText());
		gender = (String) rb.getText();
		mSex=gender;
	}
}
